package com.wuchao.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuchao.entity.User;
import com.wuchao.exception.BusinessException;
import com.wuchao.response.Result;
import com.wuchao.response.ResultCode;
import com.wuchao.service.UserService;
import com.wuchao.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wuchao
 * @since 2022-03-29
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Resource
    UserService userService;

    @ApiOperation(value ="查询信息")
    @GetMapping("/getUsers")
    public List<User> getUsers(){
        List<User> list = userService.list();
        return  list;
    }
    @ApiOperation(value ="统一结果查询信息")
    @GetMapping("/getUsersResult")
    public Result getUsersResult(){
        int i=1/0;//异常处理的
        List<User> list = userService.list();

      return  Result.ok().data("users",list);//全用OK 方法，
    }
    @ApiOperation("通过id")
    @GetMapping("/getById")
    public  Result getById(Integer id){
        User user = userService.getById(id);
        if(user==null){//处理业务异常的报错抛出，给business
            throw  new BusinessException(ResultCode.PARAM_NOT_VALID.getCode(),
                    ResultCode.PARAM_NOT_VALID.getMessage());

        }
        return Result.ok().data("user",user);
    }
    @ApiOperation(value = "分页多表条件查询")
    @PostMapping("/findUserList")
    public  Result findUserList(@RequestParam(value = "current",required = true,defaultValue = "1")
                                Integer current,
                                @RequestParam(value = "size",required = true,defaultValue = "7")
                                        Integer size,@RequestBody(required = false) UserVO userVO){
        Page<User> page=new Page<>(current,size);
        Page<User> userPage = userService.findUserList(page, userVO);
        return  Result.ok().data("userPage",userPage);
    }
    @ApiOperation(value = "分页多表条件查询1没用json")
    @GetMapping("/findUserList1")
    public  Result findUserList1(@RequestParam(value = "current",required = true,defaultValue = "1")
                                        Integer current,
                                @RequestParam(value = "size",required = true,defaultValue = "7")
                                        Integer size, UserVO userVO){
        Page<User> page=new Page<>(current,size);
        Page<User> userPage = userService.findUserList(page, userVO);
        return  Result.ok().data("userPage",userPage);
    }
    @ApiOperation(value = "导出用户信息")
    @PostMapping(value = "/export")
    public void exportUserExcel(HttpServletResponse response, @RequestBody(required = false) UserVO userVO) throws IOException {
       // QueryWrapper<User> wrapper = getWrapper(userVO);
        //这里必须写出人数，不然默认十条，但是不能写死，所以默认拿到最大值
        Integer count = userService.getBaseMapper().selectCount(null);//拿到最大值
        Page<User> userPage = new Page<>(1,count);
        Page<User> userList = userService.findUserList(userPage,userVO);

        List<User> records = userList.getRecords();
        //List<User> users = userService.list(wrapper);
        userService.exportUserExcel(response,records);
    }
    //登录
    @ApiOperation(value = "用户登录",notes = "接收用户和密码")
    @PostMapping("login")
    public  Result login(@RequestParam(value = "username",required = true) String username,
                         @RequestParam(value = "password",required = true) String password,
                         @RequestParam(value = "verifyCode",required = true) String verifyCode,
                         HttpServletRequest request){
        //调用官网-bug
       /* if (!CaptchaUtil.ver(verifyCode, request)) {
            CaptchaUtil.clear(request);  // 清除session中的验证码
            return Result.error().message("验证码不正确");
        }*/
        System.out.println(username+""+password);
        String token=userService.login(username,password);
        System.out.println(token);
        return  Result.ok().data("token",token);
    }

}

